using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Chat.Util;

namespace Chat.Client
{
    public static class Handle
    {

        public static void doRequest(byte[] data, ChatClientAsync client)
        {
            try
            {
                byte[] bHead = Tools.getHeadBytes(data);
                Head h = Tools.getHead(bHead);
                byte[] bBody = Tools.getBodyBytes(data, h.len);
                int flag = h.flag;
                switch (flag)
                {
                    case Config.C_LOGIN:
                        updateUserInfo(1, bBody, client);
                        break;
                    case Config.C_USERLIST:
                        loadUserList(h, bBody, client);
                        break;
                    case Config.C_SEND:
                        showMsg(h, bBody, client);
                        break;
                    case Config.C_ONLINE:
                        updateUserInfo(6, bBody, client);
                        break;
                    case Config.C_OFFLINE:
                        updateUserInfo(7, bBody, client);
                        break;

                }
            }
            catch (Exception e)
            {

            }

        }

        public static void updateUserInfo(int type, byte[] bBody, ChatClientAsync client)
        {
            User u = MessageReceive.getUser(bBody);
            client.UpdateMain(type, u);
        }

        public static void loadUserList(Head h, byte[] bBody, ChatClientAsync client)
        {
            User[] ul = MessageReceive.getUserList(bBody);
            client.UpdateMain(2, ul);
        }

        public static void showMsg(Head h, byte[] bBody, ChatClientAsync client)
        {
            FromMsg fm = MessageReceive.getFromString(h, bBody);
            client.UpdateMain(4, fm);
        }
    }
}
